﻿/*
    This file is part of News Worm.

    News Worm is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    News Worm is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with News Worm.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Linq;
using System.Collections.Generic;

namespace NewsWorm {
    public class CombinedSearchFeed : ICustomFeed {
        public CombinedSearchFeed( List<string> SearchResultFeedUrls, string SearchTerm, string title, int max, string UrlGuid, ScriptingEvents events) {

            m_Items = new List<FeedItem>();
            m_Title = title;
            m_Desc = "Custom feed created from search using search term \"" + SearchTerm + "\".";
            m_SearchTerm = SearchTerm;
            m_UrlGuid = UrlGuid;
            CombineAndRefresh( SearchResultFeedUrls, max, events );
        }

        private List<FeedItem> m_Items;
        private string m_Title;
        private string m_Desc;
        private List<string> m_Urls;
        private string m_SearchTerm;
        private string m_UrlGuid;

        internal string UrlGuid {
            get { return m_UrlGuid; }
        }

        public List<string> Urls {
            get { return m_Urls; }
        }

        public string Title {
            get { return m_Title; }
        }

        public string Description {
            get { return m_Desc; }
        }

        internal string SearchTerm {
            get { return m_SearchTerm; }
        }

        internal List<FeedItem> Items {
            get { return m_Items; }
        }

        private bool SearchMatch( List<string> SearchTerms, string title ) {
            SearchTerms = SearchTerms.ConvertAll( term => term.ToLower() );
            title = title.ToLower();
            return SearchTerms.All( s => title.Contains( s ) );
        }

        //set how many, also update the dialog description. for
        //custom feeds , max count is for showing how many feeds to show.
        //it might lead to a situation where all search results are not shown in the feed
        public void CombineAndRefresh( List<string> SearchResultFeedUrls, int max, ScriptingEvents events) {
            m_Urls = SearchResultFeedUrls;
            m_Items.Clear();
            int count = 0;
            
            if ( max == 0 )
                max = Properties.Settings.Default.ArticlesToFetch;

            string[] SearchTermsArray = SearchTerm.Split( ' ' );
            List<string> SearchTerms = new List<string>();

            foreach ( string s in SearchTermsArray ) {
                if ( !s.Equals( "" ) )
                    SearchTerms.Add( s );
            }

            foreach ( string url in SearchResultFeedUrls ) {
                Feed feed = new Feed( url, 0, events);
                foreach ( FeedItem item in feed.Items ) {
                    if ( SearchMatch(SearchTerms, item.Title ) ){ 
                        if ( count < max ) {
                            m_Items.Add( item );
                            count++;
                        }
                    }
                }
            }

           m_Items = m_Items.OrderByDescending( item => item.ArticlePublished ).ToList();
        }

    
        public List<IArticle> Articles {
            get {

                List<IArticle> articles = new List<IArticle>();

                foreach ( FeedItem item in m_Items ) {
                    articles.Add( item );
                }
                
                return articles; 
            
            }
        }
    }
}
